package game.players.user.movements;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Sprite;
import com.uqbar.vainilla.events.constants.Key;

import game.players.SoccerPlayer;
import game.players.user.BeatenUserPlayer;
import game.players.user.WithBallUser;

public class WithBallStationaryUser extends WithBallUser {

	public WithBallStationaryUser(SoccerPlayer player) {
		super(player);
		this.getPlayer().setAppearance(Sprite.fromImage("/images/goal3_caminando2.png").crop(0, 1, 34, 65).scale(1.5));
		this.getSoccerBall().locateBall(this.getPlayer().getX() + this.getPlayer().getAppearance().getWidth()
                - this.getSoccerBall().getX() - this.getSoccerBall().getRatio(), 0);
	}
	
	@Override
	public void update(DeltaState deltaState) {
		
		if(deltaState.isKeyBeingHold(Key.LEFT) && this.getPlayer().getX() > 0)
			this.getPlayer().setState(new WithBallLeftMovingUser(this.getPlayer(), -this.getPlayer().getMovementSpeed(), 0));
		
		if(deltaState.isKeyBeingHold(Key.RIGHT) && this.getPlayer().getX() < this.getPlayer().getGame().getDisplayWidth() - this.getPlayer().getAppearance().getWidth())
			this.getPlayer().setState(new WithBallRightMovingUser(this.getPlayer(), this.getPlayer().getMovementSpeed(), 0));

		if(deltaState.isKeyBeingHold(Key.UP) && this.getPlayer().getY() > 0)
			this.getPlayer().setState(new WithBallUpMovingUser(this.getPlayer(), 0, -this.getPlayer().getMovementSpeed()));
		
		if(deltaState.isKeyBeingHold(Key.DOWN) && this.getPlayer().getY() < this.getPlayer().getGame().getDisplayHeight() - this.getPlayer().getAppearance().getHeight())
			this.getPlayer().setState(new WithBallDownMovingUser(this.getPlayer(), 0, this.getPlayer().getMovementSpeed()));
		
		super.update(deltaState);
	}

	@Override
	public boolean isRight() {
		return true;
	}

	@Override
	public void loseTheBall() {
		this.getPlayer().setState(new BeatenUserPlayer(this.getPlayer(), this.isRight()));
	}

}
